﻿<?php 
include_once($_SERVER['DOCUMENT_ROOT'].'/wp-config.php' );
if(!empty($_GET['xoa'])){
	global $wpdb;
	$wpdb->query( "DELETE FROM ttcn_danhba WHERE ID = " . $_GET['xoa']);
}
function themDanhBa(){	
	global $wpdb;
	$errors = array();
	if(empty($_POST['input-hoten'])){
		$errors[] = "hoten";
	}
	if(empty($_POST['input-sdt'])){
		$errors[] = "sdt";
	}
	if(empty($_POST['input-email'])){
		$errors[] = "email";
	}
	if(empty($_POST['input-diachi'])){
		$errors[] = "diachi";
	}
	if(empty($errors)){
		$wpdb->insert( ttcn_danhba, array(
			'Ten' => $_POST['input-hoten'],
			'SoDt' =>$_POST['input-sdt'] ,
			'Email'=>$_POST['input-email'],
			'NgaySinh'=>$_POST['input-ngaysinh'],
			'DiaChi'=>$_POST['input-diachi'],
			'NgayTao'=>date('Y/m/d H:i:s'),
			)
		);
		$message= "<p class='success'>Đã thêm vào Danh bạ</p>";
	}
    if(!empty($message))
        echo $message;
}
function suaDanhBa($idSua){	
	global $wpdb;
	$errors = array();
	if(empty($_POST['input-hoten'])){
		$errors[] = "hoten";
	}
	if(empty($_POST['input-sdt'])){
		$errors[] = "sdt";
	}
	if(empty($_POST['input-email'])){
		$errors[] = "email";
	}
	if(empty($_POST['input-diachi'])){
		$errors[] = "diachi";
	}
	if(empty($errors)){
		$wpdb->update(ttcn_danhba, array(
			'Ten' => $_POST['input-hoten'],
			'SoDt' =>$_POST['input-sdt'] ,
			'Email'=>$_POST['input-email'],
			'NgaySinh'=>$_POST['input-ngaysinh'],
			'DiaChi'=>$_POST['input-diachi'],
			),array( 'ID' => $idSua)
		);
		$message= "<p class='success'>Đã sửa Danh bạ</p>";
	}
    if(!empty($message))
        echo $message;
}

function xemDanhBa($exQuery){
	global $wpdb;
	$thongtin = $wpdb->get_results("SELECT * FROM ttcn_danhba " . $exQuery . " ORDER BY NgayTao DESC");
	foreach ($thongtin as $canhan) {
		?>
		<tbody id="the-list-<?php echo $canhan->ID; ?>">
		<tr id="info-<?php echo $canhan->ID; ?>" class="type-post status-publish format-standard hentry category-uncategorized alternate iedit author-self level-0" valign="top">
			<th scope="row" class="check-column">
				<input id="cb-select-<?php echo $canhan->ID; ?>" type="checkbox" name="info" value="<?php echo $canhan->ID; ?>">
			</th>
			<td class="post-title page-title column-title">
				<strong>
					<a><?php echo $canhan->Ten; ?></a>
				</strong>
				<!-- action -->
				<div class="row-actions">
					<span class="edit">
						<a href="#" title="Chỉnh sửa" id="sua-<?php echo $canhan->ID; ?>" onClick="sua(this.id)">Sửa</a>
					| </span>
					<span class="trash">
						<a class="submitdelete" title="Xóa" href="#" id="xoa-<?php echo $canhan->ID; ?>"  onClick="xoa(this.id)">Xóa</a>
					</span>
				</div>
				<!-- end action -->
			</td>
			<td class="author column-author">
				<a><?php echo $canhan->SoDt; ?></a>
			</td>
			<td class="author column-author">
				<a><?php echo $canhan->Email; ?></a>
			</td>
			<td class="date column-date">
				<a><?php echo $canhan->NgaySinh; ?></a>
			</td>
			<td class="author column-author">
				<a><?php echo $canhan->DiaChi; ?></a>
			</td>
			<td class="date column-date"><abbr title="<?php echo date('d-m-Y H:i:s',strtotime($canhan->NgayTao)); ?>"><?php echo date('d-m-Y',strtotime($canhan->NgayTao)); ?></abbr></td>
		</tr>
		</tbody>
		
<?php
	} //end foreach
}//end xem danh ba
?>